package yxy.game.pm2.active.command.turntable;

import com.alibaba.fastjson.JSONObject;
import yxy.apple.logger.Logger;
import yxy.cherry.data.bean.actturntabledate;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_夺宝;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.active.module.subordinateclass.Turntable;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.ServerBean;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.bean.task.TaskType;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.ActiveMessage.ActTurnTableHandleRequest;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.task.TaskHandle;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;

@MessageType(proto = Command.ActTurnTableHandle, classtype = ActiveMessage.ActTurnTableHandleRequest.class)
public class CMD_Active_夺宝_Handle extends Command<ActTurnTableHandleRequest> {

	private static final Logger logger = Logger.create(CMD_Active_夺宝_Handle.class);
	
	@Override
	public void execute(MessageSource messageSource, ActTurnTableHandleRequest request) throws Exception {
		String playercode = messageSource.getTag();
		int type = request.getType();
		int num = request.getNum();	 //招募数目
		ActiveMessage.ActTurnTableHandleResponse.Builder response = ActiveMessage.ActTurnTableHandleResponse.newBuilder();
		
		ActiveModule_夺宝 am = ActiveModule_夺宝.get(playercode,false);
		if(am == null) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		
		if(type != ActiveModule_夺宝.普通夺宝 && type != ActiveModule_夺宝.高级夺宝) {
			send(messageSource, response, Result.DataError);
			logger.info(this.getClass() + " type " + type + " 不存在");
			return;
		}
		
		actturntabledate att = null;
		Map<String, actturntabledate> attmap = ActDataCenter.getAllData(actturntabledate.class, playercode);
		for (actturntabledate data : attmap.values()) {
			if(data.getType() == type) {
				att = data;
				if(num != data.getFrequency1() && num != data.getFrequency2()) {
					send(messageSource, response, Result.DataError);
					logger.info(this.getClass() + " num " + num + " 不存在");
					return;
				}
			}
		}
		
		PlayerBean pb = PlayerBean.get(playercode);
		ServerBean sb = ServerBean.get(pb.getServerId());
		
		int frequency = num;
		int vipcondition = -1;
		int costtype = -1;
		int costid = -1;
		int costnum = -1;
		int[] gettypes = att.getGettype();
		int[] getids = att.getGetid();
		int[] getnums = att.getGetnum();
 		
		int[] addnums = new int[getnums.length];
		
		Turntable tt;
		if(frequency == att.getFrequency1()) {
			vipcondition = att.getVIPcondition1();
			costtype = att.getCosttype1();
			costid = att.getCostid1();
			costnum = att.getCostnum1();
			tt = am.getTurntable1();
		}
		
		if(frequency == att.getFrequency2()) {
			vipcondition = att.getVIPcondition2();
			costtype = att.getCosttype2();
			costid = att.getCostid2();
			costnum = att.getCostnum2();
			tt = am.getTurntable2();
		}
		
		for(int i = 0; i < getnums.length; i++) {
			addnums[i] = getnums[i] * num;
		}
		
		if(pb.getViplevel() < vipcondition) {
			send(messageSource, response, Result.NoViplv);
			return;
		}
		
		Currency costcur = Currency.create(costtype, costid, costnum);
	
		boolean canhandle =	new CurrencyHandle().costCheck(playercode, costcur);
		if(!canhandle) {
			send(messageSource, response, new Result(Result.CostFail, costcur.getType().getNumber(), costcur.getDataId()));
			return;
		}
		
		try {
			new CurrencyHandle().cost(playercode, CCReason.活动_夺宝, costcur);
		} catch (CurrencyNotEnoughException e) {
			send(messageSource, response, new Result(Result.CostFail, costcur.getType().getNumber(), costcur.getDataId()));
			return;
		}
		
		JSONObject json = am.randomcur(type, num, sb.getAreaId(), pb.getName());
		ArrayList<Integer> rolls = json.getObject("rolls", ArrayList.class);
		ArrayList<Currency> curs = json.getObject("curs", ArrayList.class);
		
		Collection<Currency> collectioncurs = Currency.merge(curs);
		Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(collectioncurs);
		
		response.addAllDisplay(items);
		response.addAllRollpoint(rolls);
		response.setTurntableinfo(am.deal(sb.getAreaId(), type).build());
		
		Collection<Currency> getcurs = Currency.create(gettypes, getids, addnums);
		
		ArrayList<Currency> addcurs = new ArrayList<Currency>();
		for(Currency c : collectioncurs) {
			addcurs.add(c);
		}
		for(Currency c : getcurs) {
			addcurs.add(c);
		}
//		Collection<Currency> ccurs = Currency.merge(curs);
		
		TaskHandle.handle(playercode, TaskType.统计型_夺宝_次数, num);
		
		new CurrencyHandle().addCurrency(playercode, CCReason.活动_夺宝, addcurs);
		
		am.docheck();
		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, ActiveMessage.ActTurnTableHandleResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}

}
